package com.tlsy.commerce.utils;

import org.eclipse.persistence.config.SessionCustomizer;
import org.eclipse.persistence.internal.databaseaccess.Accessor;
import org.eclipse.persistence.internal.sessions.AbstractSession;
import org.eclipse.persistence.sequencing.Sequence;
import org.eclipse.persistence.sessions.Session;

import java.util.Vector;

/**
 * 自定义主键生成策略
 * @author Tlsy
 * @version commerce 0.0.1
 * @date 2017/4/10  18:04
 */
public class UUID extends Sequence implements SessionCustomizer {
    private static final long serialVersionUID = 6618546762592535635L;

    public UUID() {
        super();
    }

    public UUID(String name) {
        super(name);
    }

    public void customize(Session session) throws Exception {

        UUID uuid = new UUID("system-uuid");

        session.getLogin().addSequence(uuid);
    }

    public boolean shouldAcquireValueAfterInsert() {
        return false;
    }

    public boolean shouldUseTransaction() {
        return false;
    }
    @Override
    public boolean shouldUsePreallocation() {
        return false;
    }

    public Object getGeneratedValue(Accessor accessor, AbstractSession abstractSession, String s) {
        return java.util.UUID.randomUUID().toString().replaceAll("-","");
    }

    public Vector getGeneratedVector(Accessor accessor, AbstractSession abstractSession, String s, int i) {
        return null;
    }

    public void onConnect() {

    }

    public void onDisconnect() {

    }
}
